package com.koron.order.question.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.common.tools.CommonCodeTools;
import com.koron.order.common.tools.OrderCodeEnum;
import com.koron.order.question.bean.dto.QuestionStatus;
import com.koron.order.question.bean.po.QuestionPo;
import com.koron.order.question.bean.query.QuestionQuery;
import com.koron.order.question.bean.vo.QuestionVo;
import com.koron.order.question.mapper.QuestionMapper;
import com.koron.order.question.service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 重大问题上报单(Question)表服务实现类
 *
 * @author zhouj
 * @since 2022-01-14 10:15:29
 */
@Service("questionService")
public class QuestionServiceImpl implements QuestionService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public QuestionPo insertOrUpdate(SessionFactory factory, QuestionPo question) {

        QuestionMapper mapper = factory.getMapper(QuestionMapper.class);

        if (StringUtils.isEmpty(question.getId())) {
            //设置编号规则
            question.setCode(CommonCodeTools.getNormalOrderCode(question.getOrderCode(), OrderCodeEnum.ZD));
            question.setStatus(QuestionStatus.DRAFT.getStatus());
        }
        List<String> files = question.getFiles();
        if (!CollectionUtils.isEmpty(files)){
            String file = files.stream().collect(Collectors.joining(","));
            question.setFile(file);
        }
        mapper.insertOrUpdate(question);
        return question;
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, QuestionPo question) {

        QuestionMapper mapper = factory.getMapper(QuestionMapper.class);
        List<String> files = question.getFiles();
        if (!CollectionUtils.isEmpty(files)){
            String file = files.stream().collect(Collectors.joining(","));
            question.setFile(file);
        }
        return mapper.update(question);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        QuestionMapper mapper = factory.getMapper(QuestionMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public QuestionPo queryById(SessionFactory factory, String id) {

        QuestionMapper mapper = factory.getMapper(QuestionMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<QuestionVo> queryList(SessionFactory factory, QuestionQuery questionQuery) {

        QuestionMapper mapper = factory.getMapper(QuestionMapper.class);

        List<QuestionVo> list = mapper.queryList(questionQuery);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo<QuestionVo> queryPageList(SessionFactory factory, QuestionQuery questionQuery, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy(false));
        QuestionMapper mapper = factory.getMapper(QuestionMapper.class);

        List<QuestionVo> list = mapper.queryList(questionQuery);

        return new PageInfo(list);
    }

    /*
     * 功能描述 提交工单状态接口
     * @author zhouj
     * @date 2021/12/21 10:55
     * @param [factory, orderId:工单id, status:将要改变的状态]
     * description 对于状态的修改需要进行顺序的判断,只能往后流转,
     * @return int
     */
    @Override
    @TaskAnnotation("submitQuestion")
    public int submitQuestion(SessionFactory factory, String orderId, String status) {

        QuestionMapper mapper = factory.getMapper(QuestionMapper.class);
        //根据工单id 先判断工单当时的状态
        QuestionPo preQuestionPo = mapper.queryById(orderId);
        String preStatus = preQuestionPo.getStatus();

        //将要改变的状态
        QuestionStatus questionStatus = QuestionStatus.getQuestionStatus(status);
        Assert.notNull(questionStatus, "状态修改,枚举不能为空");

        if (StringUtils.isNotEmpty(preStatus)) {
            QuestionStatus preQuestionStatus = QuestionStatus.getQuestionStatus(preStatus);
            Assert.notNull(preQuestionStatus, "数据有误");
            Assert.isTrue(questionStatus.getSort() - preQuestionStatus.getSort() == 1, "不可越级提交");
        } else {
            Assert.isTrue(StringUtils.equalsAny(questionStatus.getStatus(),
                    QuestionStatus.DRAFT.getStatus(),
                    QuestionStatus.WAIT_REVIEW.getStatus()), "修改工单状态失败");
        }
        QuestionPo po = new QuestionPo();
        po.setId(orderId);
        po.setStatus(status);

        return mapper.update(po);
    }

}